package cms.mager.service.impl;

import cms.common.CodeMsg;
import cms.common.PageData;
import cms.common.Result;
import cms.common.SqlSessionAndMapper;
import cms.mager.domain.Customer;
import cms.mager.mapper.CustomerMapper;
import cms.mager.service.CustomerService;
import cms.util.SqlSessionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.util.List;
import java.util.Map;

/**
 * 功能:
 * Author: yuhui
 * Date: 2021/4/9 21:35
 */
public class CustomerServiceImpl implements CustomerService {

    /*1、分页查询*/
    @Override
    public Result getpage(Integer page,Integer pagesize,Customer customer) {
        SqlSessionAndMapper<CustomerMapper> mapper = SqlSessionUtil.getMapper(CustomerMapper.class);
        Page<Customer> objects = PageHelper.startPage(page, pagesize);
        mapper.getObj().pageCustomer(customer);
        PageData<Customer> customerPageData = new PageData<>();
        customerPageData.setPage(page);
        customerPageData.setPageSize(pagesize);
        customerPageData.setPageCount((int)objects.getTotal());
        customerPageData.setPageNum(objects.getPages());
        customerPageData.setLis(objects.getResult());
        SqlSessionUtil.commitAndclose(mapper.getSqlSession());
        return new Result(customerPageData);
    }

    /*2、添加客户*/
    @Override
    public Result addData(Customer customer) {
        SqlSessionAndMapper<CustomerMapper> mapper = SqlSessionUtil.getMapper(CustomerMapper.class);
        int i = mapper.getObj().addCustomer(customer);
        SqlSessionUtil.commitAndclose(mapper.getSqlSession());
        Result rt = null;
        if(i==1){
            rt = new Result();
        }else{
            rt = new Result(CodeMsg.ERROR_CUSTOMER_FIND);
        }
        return rt;
    }

    /*3、更新客户信息*//*[就更手机号吧]*/
    @Override
    public Result updateData(Customer customer) {
        SqlSessionAndMapper<CustomerMapper> mapper = SqlSessionUtil.getMapper(CustomerMapper.class);
        int i = mapper.getObj().updateInfo(customer);
        SqlSessionUtil.commitAndclose(mapper.getSqlSession());
        Result rt = null;
        if(i == 1){
            rt = new Result();
        }else{
            rt = new Result(CodeMsg.ERROR_CUSTOMER_UPDATE);
        }
        return rt;
    }

    /*4、删除客户信息*/
    @Override
    public Result delData(int id) {
        SqlSessionAndMapper<CustomerMapper> mapper = SqlSessionUtil.getMapper(CustomerMapper.class);
        int i = mapper.getObj().delCustormer(id);
        SqlSessionUtil.commitAndclose(mapper.getSqlSession());
        Result rt = null;
        if(i == 1){
            rt = new Result();
        }else{
            rt = new Result(CodeMsg.ERROR_CUSTOMER_UPDATE);
        }
        return rt;
    }

    @Override
    public Result setUserToCu(String[] cusIds, String uid) {
        SqlSessionAndMapper<CustomerMapper> mapper = SqlSessionUtil.getMapper(CustomerMapper.class);
        Result rt = null;
        try{
            mapper.getObj().updateUser(cusIds, uid);
            rt = new Result();
        }catch(Exception e){
            rt = new Result(CodeMsg.ERROR_CUSTOMER_UPDATE);
            e.printStackTrace();
        }finally {
            SqlSessionUtil.commitAndclose(mapper.getSqlSession());
        }
        return rt;
    }

}
